(function() {
    angular.module('app').factory('productControllerComponentService', productControllerComponentService);

    productControllerComponentService.$inject = ['util', '$q', 'atomicNotifyService'];

    function productControllerComponentService(util, $q, atomicNotifyService) {
        return {
            get: _get,
            getCollection: _getCollection,
            delCollection: _delCollection
        }

        function _get(json) {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/product/getProducts.htm',
                data: json
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);
            });

            return defer.promise;

        }

        function _getCollection() {
            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/getCollection.htm',
                data: {
                    type: 'product'
                }
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);
            });

            return defer.promise;
        }


        function _delCollection(product) {

        	var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/deleteCollections.htm',
                data: {
                    targetId: product.numberId,
                    type: 'product'
                }
            }).then(function(resp) {
                product.collectionStatus = null;
                var message = '取消监控' + product.title;
                atomicNotifyService.info(message);

                defer.resolve();
            }, function(err) {
                var message = '取消监控' + product.title + '失败!';
                atomicNotifyService.warning(message);

                defer.reject();
            });

            return defer.promise;
        }
    }
})();
